package com.letter.otools.day.user;

import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO;
import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES;

import android.app.AlertDialog;
import android.app.UiModeManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.appcompat.widget.Toolbar;

import com.letter.otools.day.Anniversary;
import com.letter.otools.day.BaseActivity;
import com.letter.otools.day.Constants;
import com.letter.otools.day.R;
import com.letter.otools.day.util.AppUtils;
import com.letter.otools.day.util.WebViewUtils;
import com.letter.otools.day.view.KeyValueView;


/**
 * @Description: 设置
 * @Author: dinghu
 * @CreateDate: 2021/12/24 10:20
 */
public class SettingActivity extends BaseActivity implements View.OnClickListener {
    KeyValueView version;
    KeyValueView clearCache;
    KeyValueView deal;
    KeyValueView about;
    KeyValueView themeKv;
    private ActionBar actionBar;

    @Override
    protected int getLayout() {
        return R.layout.activity_user_setting;
    }

    void initToolBar() {
        Toolbar toolbar = findViewById(R.id.Toolbar);
        setSupportActionBar(toolbar);
//        this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

        actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAsUpIndicator(AppUtils.getNightMode(this) == MODE_NIGHT_YES ? R.drawable.ic_toolbar_back_white_24dp : R.drawable.ic_toolbar_back_gray_24dp);
            actionBar.setTitle("Settings");
        }
    }

    @Override
    protected void initView() {//, BuildConfig.T
        initToolBar();
        version.setValue(AppUtils.getAppVersionName(this, getPackageName()));
        themeKv.setValue(AppUtils.getNightMode(this) == MODE_NIGHT_YES ? "Dark" : "Light");
        try {
//            clearCache.setValue(DataClearManager.getTotalCacheSize(this));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void initBinder() {
        version = findViewById(R.id.version);
        clearCache = findViewById(R.id.clearCache);
        deal = findViewById(R.id.deal);
        about = findViewById(R.id.about);
        themeKv = findViewById(R.id.themeKv);
        version.setOnClickListener(this);
        clearCache.setOnClickListener(this);
        deal.setOnClickListener(this);
        about.setOnClickListener(this);
        themeKv.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.about) {
            startActivity(new Intent(this, AboutUsActivity.class));
        } else if (id == R.id.version) {
        } else if (id == R.id.clearCache) {
        } else if (id == R.id.deal) {
            WebViewUtils.goWeb(this, "Privacy Policy", Constants.privacyUrl);
        } else if (id == R.id.themeKv) {
            String[] typeText = {
                    "Light",
                    "Dark"
            };
            AlertDialog.Builder themeDialog = new AlertDialog.Builder(this)
                    .setItems(typeText, (dialog, which) -> {
                        AppUtils.setNightMode(this, which == 0 ? MODE_NIGHT_NO : MODE_NIGHT_YES);
                    });
            themeDialog.create().show();
        }
    }
}
